/*实现 tree  gridta 之间数据互相选择的功能
其中数据格式约定
1. tree 的数据格式要求:叶子节点attributes 属性不能为空,因为 datagrid 数据均取自改属性
2. tree 节点id 同 ,atrributes中的id 一致
*/
var lrSelectDg;//保存dg easyui 对象
var lrSelectTree;//保存 tree easyui对象
var lrGridColumns;
var lrTreeList;
var lrTreeUrl='';
var lrTreeQueryParams=new Object;
var lrDlgId;
var lrCallFunction;

//save grid data
var lrGridData=new Array();
//save checked tree attributes
var lrResultArray=new Array();

function reloadLrDlg(leftRightSelect_Obj){
	lrTreeUrl=leftRightSelect_Obj.treeUrl;
	lrTreeQueryParams=leftRightSelect_Obj.lrDlgQueryParams;
	lrGridColumns=leftRightSelect_Obj.gridColumnList;
	lrTreeList=leftRightSelect_Obj.treeList;
	lrDlgId=leftRightSelect_Obj.dlgId;
	lrCallFunction=leftRightSelect_Obj.callFunction;
	
	lrGridData.length=0;
	lrResultArray.length=0;
	//清除搜索关键词
	$("#left-right-select-name").val('');
	
	console.log(lrTreeUrl);
	console.log(lrTreeQueryParams);
	
	lrSelectTree=$('#left-right-select-tree').tree({    
		//data:  lrTreeList,
	    url:lrTreeUrl,
	    parentField : 'pid',
		//lines : true,
		checkbox : true,
		/*cascadeCheck:false,*/
		/*onlyLeafCheck:true,*/
		queryParams : lrTreeQueryParams,
		onBeforeLoad:function(){
			console.log("in onBeforeLoad");
			$.messager.progress({
				title : '提示',
				text : '正在加载数据~'
			});
		},
		onLoadSuccess:function(node,data){
			console.log("in onLoadSuccess");
			console.log(data);
			console.log(JSON.stringify(data));
			//$(this).tree('collapseAll');
			$("#left-right-select-right").click();
			$.messager.progress('close');
			
		},
		onLoadError : function(node,data) { 
			console.log("in onLoadError");
	    	console.log(data);
	    	$.messager.progress('close');
	    	alert("数据加载失败，请于管理员联系:18639551912。");
	     }
	}); 
	
	
	lrSelectDg=$('#left-right-select-dg').datagrid({    
		pagination:false,
		fit:true,
	  /*	fitColumns:true,*/
	    columns:lrSelectObj.gridColumnList,
	    //data:lrGridData
	});  
	/*lrSelectDg.datagrid('reload');
	lrSelectTree.tree('reload');*/
	console.log("in reloadLrDlg");
	console.log(lrSelectObj.gridColumnList);
	//$("#left-right-select-right").click();//已经选择的显示在datagird中
}

function reloadLrDlg2(){
	lrSelectObj.gridColumnList=[[{'field':'departmentName','title':'aaa','width':'100'},
      {'field':'id','title':'id','width':'100','hidden':'true'},
      {'field':'name','title':'bb','width':'100'}]]; 
	
	
	
	lrSelectDg=$('#left-right-select-dg').datagrid({    
		pagination:false,
		fit:true,
	  	fitColumns:true,
	    columns:lrGridColumns,
	    //data:lrGridData
	});  
	lrSelectDg.datagrid('reload');
	lrSelectTree.tree('reload');
/*	console.log("in reloadLrDlg");
	console.log(lrSelectObj.gridColumnList);*/
	
}


$(function() {
	// for test
	/*var gridColumns=[[{field:'id',title:'ID',width:100},{field:'text',title:'text',width:100},]];
	var treeData= [{"id":1,"text":"Folder1","iconCls":"icon-save",
		"children":[{"id":2,"text":"File1","checked":true},
		            {"text":"Books","state":"open","attributes":{"url":"/demo/book/abc","price":100},"children":[{"text":"PhotoShop","checked":true},{"id":8,"text":"Sub Bookds","state":"closed"}]}]},{"text":"Languages","state":"closed","children":[{"text":"Java"},{"text":"C#"}]}];
	var lrDlgId='dd';
	var lrCallFunction='callFunction';*/
	
	 
	
	//注册 文本框回车事件，回车直接筛选
	$('#left-right-select-name').on('keydown',function(e){
		if(e.keyCode==13){
		  $("#left-right-select-filter").click();
		}
	});
		
	//注册筛选按钮事件
	$("#left-right-select-filter").on("click",function(){
		var rootNodes=lrSelectTree.tree('getRoots');
		$.each(rootNodes, function() {
			lrSelectTree.tree('expandAll',this.target);
		});
		lrSelectTree.tree('doFilter', $("#left-right-select-name").val());
	});
	
	
	//清除筛选按钮事件
	$("#left-right-select-reset").on("click",function(){
		console.log("clear");
		$("#left-right-select-name").val("");
		
		lrSelectTree.tree('doFilter', $("#left-right-select-name").val());
	});
	
	//注册 右选事件
	$("#left-right-select-right").on("click",function(){
		
		var nodes = lrSelectTree.tree('getChecked');
		lrGridData.length=0;
		$.each(nodes, function() {
			if(!this.attributes.ignored)
				lrGridData.push(this.attributes);
		});
		console.log(lrGridData);
		lrSelectDg.datagrid('loadData',lrGridData);
	});
	
	////注册 左选事件
	$("#left-right-select-left").on("click",function(){
		var selectionRows=lrSelectDg.datagrid('getSelections');
		console.log(selectionRows);
		$.each(selectionRows, function() {
			console.log(this.id);
			var node = lrSelectTree.tree('find', this.id);
			console.log(node);
			if(node){
				lrSelectTree.tree('uncheck', node.target);
				console.log(this.id+"unchecked.");
			}
			var rowIndex=lrSelectDg.datagrid('getRowIndex',this);
			lrSelectDg.datagrid('deleteRow',rowIndex);
		});
	});
	
	//注册 全右选事件
	$("#left-right-select-right_all").on("click",function(){
		var flag=new Object();
		flag.checked=true;
		treeChecked(flag,"left-right-select-tree");
		var nodes = lrSelectTree.tree('getChecked');
		lrGridData.length=0;
		$.each(nodes, function() {
			if(!this.attributes.ignored)
				lrGridData.push(this.attributes);
		});
		console.log(lrGridData);
		lrSelectDg.datagrid('loadData',lrGridData);
	});
	
	
	//注册 全左选事件
	$("#left-right-select-left_all").on("click",function(){
		var flag=new Object();
		flag.checked=false;
		treeChecked(flag,"left-right-select-tree");
		lrGridData.length=0;
		lrSelectDg.datagrid('loadData',lrGridData);
	});

	//注册取消事件
	$("#left-right-select-cancle").on("click",function(){
		$("#"+lrDlgId).dialog('close');
		
	});
	
	//注册OK事件
	$("#left-right-select-ok").on("click",function(){
		var nodes = lrSelectTree.tree('getChecked');	;
		
		$.each(nodes, function() {
			if(!this.attributes.ignored)
				lrResultArray.push(this.attributes);
		});
		lrCallFunction(lrResultArray);
		$("#left-right-select-cancle").click();
		/*var runString=lrCallFunction+"(lrResultArray)";
		console.log(lrResultArray);
		console.log(runString);
		eval(runString);*/
		//lrCallFunction(nodes.attributes);

	});
	
});